{
  "metadata" : {
    "config" : {
      "dependencies" : {
        
      },
      "exclusions" : [
      ],
      "repositories" : [
      ],
      "sparkConfig" : {

      }
    }
  },
  "nbformat" : 4,
  "nbformat_minor" : 0,
  "cells" : [
    {
      "cell_type" : "markdown",
      "execution_count" : 0,
      "metadata" : {
        "language" : "text"
      },
      "language" : "text",
      "source" : [
        "# Accessing a Python object in Scala\n",
        "\n",
        "\n",
        "You can call methods and properties of Python objects in Scala code. Consider the following Python class:\n",
        "\n",
        "\n"
      ],
      "outputs" : [
      ]
    },
    {
      "cell_type" : "code",
      "execution_count" : 1,
      "metadata" : {
        "cell.metadata.exec_info" : {
          "startTs" : 1572053589496,
          "endTs" : 1572053591961
        },
        "language" : "python"
      },
      "language" : "python",
      "source" : [
        "class MyPythonClass:\n",
        "    \"\"\"A simple example class\"\"\"\n",
        "    i = 12345\n",
        "\n",
        "    def f(self):\n",
        "        return 'hello world'\n",
        "    \n",
        "    def add(self, a, b):\n",
        "        return a + b\n",
        "    \n",
        "    def getLength(self, thing):\n",
        "        return len(thing)\n",
        "\n",
        "pyInst = MyPythonClass()"
      ],
      "outputs" : [
      ]
    },
    {
      "cell_type" : "markdown",
      "execution_count" : 2,
      "metadata" : {
        "language" : "text"
      },
      "language" : "text",
      "source" : [
        "Simple actions work in Scala:"
      ],
      "outputs" : [
      ]
    },
    {
      "cell_type" : "code",
      "execution_count" : 3,
      "metadata" : {
        "cell.metadata.exec_info" : {
          "startTs" : 1572053621295,
          "endTs" : 1572053621661
        },
        "language" : "scala"
      },
      "language" : "scala",
      "source" : [
        "pyInst.f() // note that this must be called with `()` like in Python. "
      ],
      "outputs" : [
        {
          "execution_count" : 3,
          "data" : {
            "text/plain" : [
              "hello world"
            ]
          },
          "metadata" : {
            "name" : "Out",
            "type" : "PythonObject"
          },
          "output_type" : "execute_result"
        }
      ]
    },
    {
      "cell_type" : "code",
      "execution_count" : 4,
      "metadata" : {
        "cell.metadata.exec_info" : {
          "startTs" : 1572053645552,
          "endTs" : 1572053645802
        },
        "language" : "scala"
      },
      "language" : "scala",
      "source" : [
        "pyInst.i"
      ],
      "outputs" : [
        {
          "execution_count" : 4,
          "data" : {
            "text/plain" : [
              "12345"
            ]
          },
          "metadata" : {
            "name" : "Out",
            "type" : "Out"
          },
          "output_type" : "execute_result"
        }
      ]
    },
    {
      "cell_type" : "code",
      "execution_count" : 5,
      "metadata" : {
        "cell.metadata.exec_info" : {
          "startTs" : 1572053654502,
          "endTs" : 1572053654879
        },
        "language" : "scala"
      },
      "language" : "scala",
      "source" : [
        "pyInst.add(1, Math.PI)"
      ],
      "outputs" : [
        {
          "execution_count" : 5,
          "data" : {
            "text/plain" : [
              "4.141592653589793"
            ]
          },
          "metadata" : {
            "name" : "Out",
            "type" : "PythonObject"
          },
          "output_type" : "execute_result"
        }
      ]
    },
    {
      "cell_type" : "code",
      "execution_count" : 6,
      "metadata" : {
        "cell.metadata.exec_info" : {
          "startTs" : 1572053660806,
          "endTs" : 1572053661095
        },
        "language" : "scala"
      },
      "language" : "scala",
      "source" : [
        "pyInst.getLength(Array(1, 2, 3))"
      ],
      "outputs" : [
        {
          "execution_count" : 6,
          "data" : {
            "text/plain" : [
              "3"
            ]
          },
          "metadata" : {
            "name" : "Out",
            "type" : "PythonObject"
          },
          "output_type" : "execute_result"
        }
      ]
    },
    {
      "cell_type" : "markdown",
      "execution_count" : 7,
      "metadata" : {
        "language" : "text"
      },
      "language" : "text",
      "source" : [
        "Note that passing Scala values into Python only works for primitive values right now. So, for example, the follow cell will fail because Python doesn't know what to do with a Scala `Seq`. "
      ],
      "outputs" : [
      ]
    },
    {
      "cell_type" : "code",
      "execution_count" : 8,
      "metadata" : {
        "cell.metadata.exec_info" : {
          "startTs" : 1572053741115,
          "endTs" : 1572053741333
        },
        "language" : "scala"
      },
      "language" : "scala",
      "source" : [
        "pyInst.getLength(Seq(1,2,3))"
      ],
      "outputs" : [
        {
          "ename" : "zio.FiberFailure",
          "evalue" : "Fiber failed.\nA checked error was not handled.\njep.JepException: <class 'TypeError'>: object of type 'jep.PyJObject' has no len()\n\tat <ast>.getLength(<ast>:12)\n\tat jep.python.PyCallable.call(Native Method)\n\tat jep.python.PyCallable.callAs(PyCallable.java:110)\n\tat polynote.runtime.python.PythonObject$$anonfun$1.apply(PythonObject.scala:23)\n\tat polynote.runtime.python.PythonObject$$anonfun$1.apply(PythonObject.scala:22)\n\tat zio.blocking.Blocking$Service$$anonfun$effectBlocking$1$$anonfun$apply$3.apply(Blocking.scala:133)\n\tat zio.blocking.Blocking$Service$$anonfun$effectBlocking$1$$anonfun$apply$3.apply(Blocking.scala:127)\n\tat zio.internal.FiberContext.evaluateNow(FiberContext.scala:333)\n\tat zio.internal.FiberContext.zio$internal$FiberContext$$run$body$2(FiberContext.scala:602)\n\tat zio.internal.FiberContext$$anonfun$7.run(FiberContext.scala:602)\n\tat java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)\n\tat java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)\n\tat java.lang.Thread.run(Thread.java:748)\n\nFiber:35501 was supposed to continue to:\n  a future continuation at zio.blocking.Blocking$Service$$anonfun$effectBlocking$1$$anonfun$apply$4$$anonfun$apply$5.apply(Blocking.scala:145)\n  a future continuation at zio.ZIO$$anonfun$ensuring$1$$anonfun$apply$13.apply(ZIO.scala:317)\n  a future continuation at zio.blocking.Blocking$Service$$anonfun$effectBlocking$1$$anonfun$apply$7.apply(Blocking.scala:126)\n  a future continuation at zio.ZIO$$anonfun$run$2.apply(ZIO.scala:1120)\n  a future continuation at zio.ZIO$$anonfun$bracket_$1.apply(ZIO.scala:144)\n  a future continuation at zio.ZIO$$anonfun$run$2.apply(ZIO.scala:1120)\n  a future continuation at zio.ZIO$$anonfun$bracket_$1.apply(ZIO.scala:144)\n  a future continuation at polynote.kernel.interpreter.python.PythonInterpreter$$anon$3$$anonfun$run$1.apply(PythonInterpreter.scala:46)\n\nFiber:35501 execution trace:\n  at zio.ZIO$$anonfun$flatten$1.apply(ZIO.scala:402)\n  at zio.ZIO$ZipLeftFn$$anonfun$apply$172.apply(ZIO.scala:2665)\n  at zio.UIO$$anonfun$effectSuspendTotal$1.apply(UIO.scala:183)\n  at zio.Fiber$$anonfun$join$2.apply(Fiber.scala:69)\n  at zio.Fiber$$anonfun$join$1.apply(Fiber.scala:69)\n  at zio.internal.FiberContext$$anonfun$await$1.apply(FiberContext.scala:618)\n  at zio.blocking.Blocking$Service$$anonfun$effectBlocking$1$$anonfun$apply$4.apply(Blocking.scala:127)\n  at zio.ZIOFunctions$$anonfun$effectSuspendTotal$1.apply(ZIO.scala:1935)\n  at polynote.runtime.python.PythonObject$$anonfun$1.apply(PythonObject.scala:22)\n  at zio.blocking.package$$anonfun$effectBlocking$1.apply(blocking.scala:34)\n  at zio.ZIO$$anonfun$bracket_$2.apply(ZIO.scala:144)\n  at zio.internal.FiberContext$$anonfun$lock$2.apply(FiberContext.scala:543)\n  at zio.internal.FiberContext$$anonfun$lock$1.apply(FiberContext.scala:543)\n  at zio.ZIO$$anonfun$bracket_$2.apply(ZIO.scala:144)\n  at zio.internal.FiberContext$$anonfun$1.apply(FiberContext.scala:471)\n\nFiber:35501 was spawned by: <empty trace>",
          "traceback" : [
          ],
          "output_type" : "error"
        }
      ]
    },
    {
      "cell_type" : "code",
      "execution_count" : 9,
      "metadata" : {
        "language" : "scala"
      },
      "language" : "scala",
      "source" : [
      ],
      "outputs" : [
      ]
    }
  ]
}